package com.example.commuity.attention

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.example.common.mvvm.BaseMvvmFragment
import com.example.commuity.R
import com.example.commuity.adapter.AttentionListAdapter
import com.example.commuity.databinding.FragmentAttentionBinding
import com.example.commuity.uistate.CommunityUiState
import com.example.commuity.viewmodel.CommunityViewModel
import com.example.entity.BaseEntity
import com.example.recyclerview.RecyclerItemDecoration
import com.example.video.helper.ScrollCalculatorHelper
import com.scwang.smart.refresh.header.ClassicsHeader
import com.shuyu.gsyvideoplayer.GSYVideoManager
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AttentionFragment : BaseMvvmFragment<FragmentAttentionBinding>() {

    private val mViewModel :CommunityViewModel by activityViewModels()

    private lateinit var mAttentionListAdapter : AttentionListAdapter
    private lateinit var mScrollerCalculatorHelper: ScrollCalculatorHelper
    private lateinit var mLayoutManger :LinearLayoutManager


    private var isLoadMore = false
    private var isFullPlay =false

    companion object {
        @JvmStatic
        fun newInstance() = AttentionFragment()
    }

    override fun getLayoutId(): Int =R.layout.fragment_attention

    override fun initViews() {
        mDataBinding.rvAttention.setHasFixedSize(true)
        mLayoutManger= LinearLayoutManager(requireContext())
        mDataBinding.rvAttention.layoutManager=mLayoutManger

        var decorationSize = SizeUtils.dp2px(10f)
        mDataBinding.rvAttention.addItemDecoration(RecyclerItemDecoration(0,decorationSize,decorationSize,decorationSize))
        mAttentionListAdapter= AttentionListAdapter(R.layout.item_attention_card_view)
        mDataBinding.rvAttention.adapter=mAttentionListAdapter

        //下拉刷新加载更多
        mDataBinding.refreshLayout.setRefreshHeader(ClassicsHeader(requireContext()))
        mDataBinding.refreshLayout.setRefreshHeader(ClassicsHeader(requireContext()))
        mDataBinding.refreshLayout.setOnRefreshListener {
            isLoadMore=false
            mViewModel.loadAttentionData(isLoadMore)
        }
        mDataBinding.refreshLayout.setOnLoadMoreListener {
            isLoadMore=true
            mViewModel.loadAttentionData(isLoadMore)
        }


        //限定范围为屏幕一半
        var playTop = ScreenUtils.getScreenHeight()/2 -SizeUtils.dp2px(180f)
        var playBottom = ScreenUtils.getScreenHeight()/2 +SizeUtils.dp2px(180f)
        mScrollerCalculatorHelper=ScrollCalculatorHelper(R.id.video_item_player,playTop,playBottom)

        //页面滚动事件
        mDataBinding.rvAttention.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            var firstVisibleItem =0
            var lastVisibleItem =0

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                mScrollerCalculatorHelper.onScrollStateChanged(recyclerView,newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                //获取第一个和最后一个
                firstVisibleItem= mLayoutManger.findFirstVisibleItemPosition()
                lastVisibleItem=mLayoutManger.findLastVisibleItemPosition()

                //计算需要播放的视频
                if (!isFullPlay){
                    mScrollerCalculatorHelper.onScroll(recyclerView,firstVisibleItem,lastVisibleItem,lastVisibleItem-firstVisibleItem)
                }

            }
        })
    }




    override fun lazyLoadData() {
        if (mAttentionListAdapter?.data?.size==0 || mAttentionListAdapter?.data?.size==null){
            mViewModel.loadAttentionData()
        }else{
            mDataBinding.rvAttention.scrollToPosition(0)
        }
    }

    override fun setDataObserver() {
        mViewModel.mLiveData.observe(this){
            when(it){
                is CommunityUiState.loadAttentionSuccess->{upDataUi(it.data as MutableList<BaseEntity>)}
                is CommunityUiState.loadAttentionFail->showMsg(it.errorMsg)
                else->{}
            }
        }
    }

    private fun upDataUi(list: MutableList<BaseEntity>) {

        if (!isLoadMore){
            mAttentionListAdapter.setNewInstance(list)
            if (mDataBinding.refreshLayout.isRefreshing){
                mDataBinding.refreshLayout.finishRefresh()
            }
        }else{
            mAttentionListAdapter.addData(list)
            if (mDataBinding.refreshLayout.isLoading){
                mDataBinding.refreshLayout.finishLoadMore()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onResume()
    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        GSYVideoManager.releaseAllVideos()
    }

}